package com.lmx.persistence.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.repoDomain.tmodel.CustomerDrug;
import com.lmx.repoDomain.tmodel.CustomerDrugProject;
import com.lmx.repoDomain.tmodel.CustomerDrugRecord;
import com.lmx.repoDomain.tmodel.Drug;

/**
 * 药品剂型
 * 
 * @author ytche
 *
 * 
 */
@Service
@Scope("prototype")
public class DrugService {
	private Log logger = LogFactory.getLog(this.getClass());
	@PersistenceContext
	private EntityManager em;

	@Autowired
	protected GenericService dslJpaService;
	
	/**
	 * 检测是否可删除
	 * 
	 * @author ytche
	 * @param drug
	 * @return ture可以删除 false不能删除
	 * @date 2017年4月10日 下午4:02:40
	 */
	public Boolean checkDelete(Drug drug) {
		String queryStr = "select c from  CustomerDrug c where c.drug=?1 and c.delStatus=0";
		List<Drug> lists = this.em.createQuery(queryStr).setParameter(1, drug)
				.getResultList();
		return lists == null || lists.size() < 1;
	}
	
	public Boolean checkCustomerDrugDelete(CustomerDrug cd) {
		String queryStr = "select c from  CustomerDrugProject c where c.customerDrug.id=?1 and c.delStatus=0";
		List<CustomerDrugProject> lists = this.em.createQuery(queryStr).setParameter(1, cd.getId())
				.getResultList();
		return lists == null || lists.size() < 1;
	}


	/**
	 * 检测重名
	 * 
	 * @author ytche
	 * @param name
	 * @param tenantId
	 * @return true 重名 false 不重名
	 * @date 2017年4月13日 下午6:02:59
	 */
	public boolean checkName(String name, Long tenantId) {
		String queryStr = "select d from Drug d where d.name= ?1 and d.delStatus= 0 and d.tenant.id=?2";
		List<Drug> lists = this.em.createQuery(queryStr).setParameter(1, name)
				.setParameter(2, tenantId).getResultList();
		return lists != null && lists.size() > 0;
	}
	
	
	
	/**
	 *   @author huibati
	 *   @param
	 *   新建老人药品 并生成药品库存记录
	 *   2017年7月31日 
	 */
	@Transactional
	public CustomerDrug createCustomerDrug(CustomerDrug data){
		Double drugNum=0.0;
		int recordFlag=0;
		//修改库存
		if (data.getAddDrugNum() != null && data.getAddDrugNum() != 0 && data.getAddDrugNum() != null) {
			recordFlag=1;
			if (CommonConstants.DICTTYPE.ADD_DRUG_TYPE_IN.label().equals(data.getAddFlag().getTypecode())) {
				drugNum +=  data.getAddDrugNum();
			}
			if (CommonConstants.DICTTYPE.ADD_DRUG_TYPE_OUT.equals(data.getAddFlag().getTypecode())) {
				drugNum -=  data.getAddDrugNum();
			}
			if(drugNum<0){
				data.setErrcode(Integer.parseInt(CommonConstants.ERROR_CODE.ADD_ERROR.label()));
				return data;
			}
		}
		data.setDrugNum(drugNum);
		dslJpaService.setEntityClass(CustomerDrug.class);
		dslJpaService.create(data);
	    //生成库存记录
		if(recordFlag==1){
			CustomerDrugRecord record=new CustomerDrugRecord();
			record.setCustomerDrug(data);
			record.setAddDrugNum(data.getAddDrugNum());
			record.setAddType(data.getAddFlag());
			record.setDrugNum(drugNum);
			record.setGatherStaff(data.getGatherStaff());
			record.setCreateDateTime(CommonUtil.getCurrTimes());
			dslJpaService.setEntityClass(CustomerDrugRecord.class);
			dslJpaService.create(record);	
			dslJpaService.setEntityClass(CustomerDrug.class);
		}
		return data;
	}
	
	/**
	 *   @author huibati
	 *   @param
	 *   修改老人药品 并生成药品库存记录
	 *   2017年7月31日 
	 */
	@Transactional
	public CustomerDrug updateCustomerDrug(CustomerDrug data,short delStatus){
		Double drugNum = 0.0;
		if (data.getDrugNum() != null) {
			drugNum = data.getDrugNum();
		}
		//修改库存
		if (delStatus == 0 && data.getAddDrugNum() != null && data.getAddDrugNum() != 0 && data.getAddDrugNum() != null) {
			if (CommonConstants.DICTTYPE.ADD_DRUG_TYPE_IN.label().equals(data.getAddFlag().getTypecode())) {
				data.setDrugNum(drugNum + data.getAddDrugNum());
				drugNum +=  data.getAddDrugNum();
			}
			if (CommonConstants.DICTTYPE.ADD_DRUG_TYPE_OUT.label().equals(data.getAddFlag().getTypecode())) {
				data.setDrugNum(drugNum - data.getAddDrugNum());
				drugNum -=  data.getAddDrugNum();
			}
			if(drugNum<0){
				data.setErrcode(Integer.parseInt(CommonConstants.ERROR_CODE.ADD_ERROR.label()));
				return data;
			}
		    //生成库存记录
			CustomerDrugRecord record=new CustomerDrugRecord();
			record.setCustomerDrug(data);
			record.setAddDrugNum(data.getAddDrugNum());
			record.setAddType(data.getAddFlag());
			record.setDrugNum(drugNum);
			record.setGatherStaff(data.getGatherStaff());
			record.setCreateDateTime(CommonUtil.getCurrTimes());
			dslJpaService.setEntityClass(CustomerDrugRecord.class);
			dslJpaService.create(record);
		}
		dslJpaService.setEntityClass(CustomerDrug.class);
		CustomerDrug oldData = (CustomerDrug)dslJpaService.findOne(data.getId());
		oldData.setDrugNum(data.getDrugNum());
		oldData.setRemark(data.getRemark());
		oldData.setDelStatus(delStatus); 
		dslJpaService.setEntityClass(CustomerDrug.class);
		dslJpaService.update(oldData);
		return data;
	}
}
